package com.regiee.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.regiee.common.R;
import com.regiee.entity.Category;
import com.regiee.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    /*
    新增菜品分类接口
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增分类成功");
    }
    /*
    分类信息分页查询接口
     */
    @GetMapping("/page")
    public R<Page<Category>> page(int page,int pageSize){
        //创建分页查询
        Page<Category> pageInfo=new Page<>(page,pageSize);
        //构造分页查询条件
        LambdaQueryWrapper<Category> wrapper =new LambdaQueryWrapper<Category>()
                .orderByAsc(Category::getSort);
        categoryService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }
    /*
    修改分类接口
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改分类成功");
    }
    /*
    删除分类接口，需要判断该类菜品下是否有菜
     */
    @DeleteMapping
    public R<String> delete(@RequestParam("ids") Long id){
        //首先判断该类菜品下是否有菜

//        categoryService.removeById(id);
        categoryService.remove(id);
    return R.success("删除分类成功");
    }
    /*
    根据条件来查询分类数据
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        log.info(category.toString());
        LambdaQueryWrapper<Category> wrapper=new LambdaQueryWrapper<Category>()
                .eq(category.getType()!=null,Category::getType,category.getType())
                .orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list=categoryService.list(wrapper);
        return R.success(list);
    }
}
